package com.educate.service.impl;

import com.educate.dao.ResponseDao;
import com.educate.pojo.Response;
import com.educate.service.ResponseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author xiongyu
 * @date 2021/12/23 19:00
 */

@Service
public class ResponseServiceImpl implements ResponseService {
    @Autowired
    ResponseDao responseDao;

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public long insertResponse(Response response) {
        responseDao.insertResponse(response.setCreatedResponseTime(new Date()).setUsername(Long.parseLong(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString())));
        return response.getResponseId();
    }

    @Override
    public boolean deleteResponse(long responseId) {
        return responseDao.deleteResponse(responseId) == 1;
    }

    @Override
    public List<Response> queryResponseByProblemId(long problemId) {
        return responseDao.queryResponseByProblemId(problemId);
    }
}
